package com.wangzhenyu.common.util.exts

import android.view.View
import android.widget.EditText
import android.widget.TextView
import com.wangzhenyu.common.R

/**D:
 * Desc   : 操作View的扩展
 */
fun View.gone() {
    visibility = View.GONE
}

fun View.visible() {
    visibility = View.VISIBLE
}

fun View.show(isShow: Boolean) {
    visibility = if (isShow) View.VISIBLE else View.GONE
}

fun View.inVisible() {
    visibility = View.INVISIBLE
}

fun TextView.getViewText(): String {
    return this.text.toString().trim()
}

fun EditText.getViewText(): String {
    return this.text.toString().trim()
}


// --------------------- View 防多次点击 ---------------------


//设置单击按钮的间隔时间    internel
@Suppress("UNCHECKED_CAST")
fun <T : View> T.singleClick(time: Long = 100, block: (T) -> Unit) {
    triggerDelay = time
    setOnClickListener {
        if (clickEnable()) {
            block(it as T)
        }
    }
}

private var <T : View> T.triggerLastTime: Long
    get() = if (getTag(R.id.viewLastTriggerTime) != null) getTag(R.id.viewLastTriggerTime) as Long else 0
    set(value) = setTag(R.id.viewLastTriggerTime, value)

private var <T : View> T.triggerDelay: Long
    get() = if (getTag(R.id.viewTriggerDelay) != null) getTag(R.id.viewTriggerDelay) as Long else -1
    set(value) = setTag(R.id.viewTriggerDelay, value)

private fun <T : View> T.clickEnable(): Boolean {
    var flag = false
    val currentClickTime = System.currentTimeMillis()
    if (currentClickTime - triggerLastTime >= triggerDelay) flag = true
    triggerLastTime = currentClickTime
    return flag
}